﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "BalloonTipManager.h"
#include "GbmpNew.h"

#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"

namespace
{
const std::wstring DefaultOwnerWidgetPromptStyle = L"{ border: 2px solid #FF3333 }";
}

namespace gcmp
{

BalloonTipManager& BalloonTipManager::GetInstance()
{
    static BalloonTipManager balloonManager;
    return balloonManager;
}

void BalloonTipManager::DetachBalloon()
{
    m_pBalloon = nullptr;
}

void BalloonTipManager::ShowBalloon(QWidget* pOwnerWidget, const std::wstring& message, int timeout)
{
    QtBalloonTip::Parameter parameter;
    parameter.message = message;
    parameter.timeout = timeout;

    BalloonTipManager::ShowBalloon(pOwnerWidget, parameter);
}

void BalloonTipManager::ShowBalloon(QWidget* pOwnerWidget, const QtBalloonTip::Parameter& parameter)
{
    if (!pOwnerWidget)
        return;

    if (m_pBalloon)
    {
        QWidget* pCurrentOwnerWidget = m_pBalloon->GetOwnerWidget();
        if (pOwnerWidget != pCurrentOwnerWidget)
        {
            m_pBalloon->ShowBalloon();
            return;
        }
    }
    else
    {
        //m_pBalloon = NEW_AS_OWNER_PTR(QtBalloonTip, pOwnerWidget, parameter);
        m_pBalloon = NEW_AS_QT(QtBalloonTip, pOwnerWidget, parameter);
        //QObject::connect(pOwnerWidget, &QWidget::destroyed, [=]() { m_pBalloon.reset(); });
    }

    m_pBalloon->ShowBalloon();
    SetOwnerWidgetStyle(DefaultOwnerWidgetPromptStyle);

    QWidget* pTopLevelWidget = pOwnerWidget->topLevelWidget();
    if (pTopLevelWidget)
    {
        QSignalBlocker blocker(pTopLevelWidget->focusWidget());
        pOwnerWidget->setFocus();
    }
    else
    {
        pOwnerWidget->setFocus();
    }
}

void BalloonTipManager::Clear()
{
    if (!m_pBalloon)
        return;

    QWidget* pOwnerWidget = m_pBalloon->GetOwnerWidget();
    if (pOwnerWidget)
        RestoreOwnerWidgetStyle();

    m_pBalloon->close();
    m_pBalloon = nullptr;
    //m_pBalloon.reset();
}

QWidget* BalloonTipManager::GetOwnerWidget()
{
    return m_pBalloon ? m_pBalloon->GetOwnerWidget() : nullptr;
}

BalloonTipManager::BalloonTipManager()
    : m_ownerWidgetOldStyle(L"")
    , m_pBalloon(nullptr)
{
}

void BalloonTipManager::SetOwnerWidgetStyle(const std::wstring& style)
{
    QWidget* pOwnerWidget = m_pBalloon->GetOwnerWidget();
    if (pOwnerWidget)
    {
        QString styleQString = QString::fromStdWString(style);
        QString oldStyleQString = pOwnerWidget->styleSheet();
        oldStyleQString = oldStyleQString.mid(oldStyleQString.indexOf("{"));
        if (styleQString != oldStyleQString)
        {
            m_ownerWidgetOldStyle = oldStyleQString.toStdWString();
            QString newStyle = QString("QWidget#%1 %2").arg(pOwnerWidget->objectName()).arg(styleQString);
            pOwnerWidget->setStyleSheet(newStyle);
        }
    }
}

void BalloonTipManager::RestoreOwnerWidgetStyle()
{
    QWidget* pOwnerWidget = m_pBalloon->GetOwnerWidget();
    if (pOwnerWidget)
        pOwnerWidget->setStyleSheet(QString::fromStdWString(m_ownerWidgetOldStyle));
}

}   // end namespace gcmp
